﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
namespace Exact_VAS
{
    public partial class UpdateFCAmountMsg : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        bool flagInsert = false;
        ISHCMC_V_BankPaymentVoucher itemBPVGlobal;
        public UpdateFCAmountMsg(ExactDataContext _DS,ISHCMC_V_BankPaymentVoucher itemBPV)
        {
            InitializeComponent();
            DS = _DS;
            itemBPVGlobal = itemBPV;
            this.LoadData();
            if (CheckExistGbkmutExt(itemBPV.ID))
            {
                var queryGE = from GE in DS.ISHCMC_GbkmutExts
                              where GE.gbkmutID == itemBPV.ID
                              select GE;
                ISHCMC_GbkmutExt itemGE = queryGE.First();

                this.txtAmount.Text = itemGE.FCAmount.Value.ToString();
                this.cbxCurrency.EditValue = itemGE.FCCurrency;
                flagInsert = false;
            }
            else
            {
                flagInsert = true;
            }
        }
        private void LoadData()
        {
            var queryCurrency = from Cur in DS.valutas
                                where Cur.active == 1
                                select Cur;
            this.valutaBindingSource.DataSource = queryCurrency;
            if (queryCurrency.Count() > 0)
            {
                this.cbxCurrency.EditValue = queryCurrency.First().valcode;
            }
        }
        private bool CheckExistGbkmutExt(int ID)
        {
            var queryGbkmutE = from GE in DS.ISHCMC_GbkmutExts
                               where GE.gbkmutID == ID
                               select GE;
            if (queryGbkmutE.Count() >0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (flagInsert)
                {
                    ISHCMC_GbkmutExt itemGE = new ISHCMC_GbkmutExt()
                    {
                        gbkmutID = itemBPVGlobal.ID,
                        FCCurrency = this.cbxCurrency.EditValue.ToString(),
                        FCAmount = Convert.ToDouble(this.txtAmount.Text)
                    };
                    DS.ISHCMC_GbkmutExts.InsertOnSubmit(itemGE);
                    DS.SubmitChanges();
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    ISHCMC_GbkmutExt itemGE = DS.ISHCMC_GbkmutExts.Single(c => c.gbkmutID == itemBPVGlobal.ID);
                    itemGE.FCCurrency = this.cbxCurrency.EditValue.ToString();
                    itemGE.FCAmount = Convert.ToDouble(this.txtAmount.Text);
                    this.DialogResult = DialogResult.OK;
                }
            }
            catch(ExecutionEngineException ex)
            {
                this.DialogResult = DialogResult.No;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}